package xj.graph2d.template.uml;

import xj.graph2d.AbstractModelListener;
import xj.graph2d.DrawShape;
import xj.graph2d.GraphNodeBase;
import xj.model.uml.UMLObject;

public class UMLObjectModelListener extends AbstractModelListener {

  public UMLObjectModelListener() {
  }

  public UMLObjectModelListener(UMLObject model, DrawShape shape) {
    super(model, shape);
  }

  @Override
  public void updateModel() {
    if (shape instanceof GraphNodeBase && model instanceof UMLObject) {
      GraphNodeBase onode = (GraphNodeBase) shape;
      UMLObject omodel = (UMLObject) model;
      String objName = null;
      String className = null;
      String label = onode.getLabel();
      if (label != null) {
	int i = label.indexOf(':');
	if (i >= 0) {
	  objName = label.substring(0, i).trim();
	  className = label.substring(i + 1).trim();
	} else {
	  objName = label.trim();
	}
      }
      omodel.setName(objName);
      omodel.setClassname(className);
    }
  }

  @Override
  public void updateShape() {
    if (shape instanceof GraphNodeBase && model instanceof UMLObject) {
      GraphNodeBase onode = (GraphNodeBase) shape;
      UMLObject omodel = (UMLObject) model;
      String objName = omodel.getName();
      String className = omodel.getClassname();
      String label = null;
      if (objName != null) {
	label = objName;
	if (className != null) {
	  label += (" : " + className);
	}
      } else {
	if (className != null) {
	  label = (" : " + className);
	}
      }
      onode.setLabel(label);
    }
  }

}
